<template>
	<view style="background-color: #fff;padding-bottom: 100rpx;">
		<div class="daycheck">
			<img :src="imgUrl" alt="" class="topbanner" mode="top center">
			<view class="gotoAlbum" @click="gotoAlbum">生成自己的专属电子相册</view>
			<!-- 	<div class="readyCheck" v-if="!isCheck">
				<button @click="checkToday">打卡说{{time}}</button>
			</div> -->
			<!-- <div class="TodayChek" v-else>
				<span class="row">
					<div class="lt">
						<em>连续打卡</em>
						<div class="Num">1 <div class="unit">天</div>
						</div>
					</div>
					<div class="rt">
						<div class="Num">11/15</div>
						<em>2023年 周五</em>
					</div>
				</span>
				<span class="row">
					<div class="lt">
						<em>今日打卡</em>
						<div class="Num">17：01</div>
					</div>
				</span>
			</div> -->
		</div>
		<view :style="{'top':top}" :class="[top==0 ? 'showtips' : '' ]" class="download-tip">{{count}}条数据</view>
		<mescroll-body-diy ref="mescrollRef" @init="mescrollInit" top="0" bottom="100" :down="downOption"
			@down="downCallback" @up="upCallback">
			<!-- <view class="adContainer">
				<ad unit-id="adunit-3a38f65b10ef65fd" ad-intervals="30"></ad>
			</view> -->
			<block v-for="(item,i) in data">
				<view class="showdate" style="margin-top:20px;">
					<view style="width: 70%;margin: auto;">
						<image class="dateimg" mode="" src="../../static/img/lefticon.png" style="height: 15.1709px;"
							role="img"></image>
						<view class="datetext" style="margin-top:-4px; width: 100px;">
							<view style="width:100%; margin:auto">{{item.lunar}}</view>
							<view style="width: 100%; margin: 0 auto; font-size:10px;color:#333333">{{item.solar}}
							</view>
						</view>
						<image class="dateimg" mode="" src="../../static/img/righticon.png" role="img"
							style="height: 14.6999px; "></image>
					</view>
				</view>
				<card v-for="(Item, index) in item.newslist" :key="index" :newstitle="Item.shorttitle"
					:fixedtime="Item.fixedtime" :newsthumb="Item.thumb" :newscode="Item.ano"
					@toNewsDetail="clickToDetail(Item.ano,Item.type)">
				</card>
			</block>

			<view class="banner">
				<!-- <image :src="banner"></image> -->
				<!-- <button @click="subscribe">点击获取祝福订阅</button> -->
			</view>
		</mescroll-body-diy>
	</view>
</template>

<script>
	//todo ***   务必记得上传前关闭调试模式   ***
	let appId = "wxc716234eca6701b2" // wx67036938335db889  wx2facf5282e5ca5a6
	// 在页面中定义插屏广告
	let interstitialAd = null
	import card from '@/components/card/card2.vue';
	// 引入mescroll-mixins.js
	import MescrollBodyDiy from "@/components/mescroll-diy/wx/mescroll-body.vue";
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	let rewardedVideoAd = null
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components: {
			card,
			MescrollBodyDiy, // 避免与main.js注册的全局组件名称相同,否则注册组件失效(iOS真机 APP HBuilderX2.7.9)
		},
		data() {
			return {
				head: '../../static/img/head.png',
				canIUseGetUserProfile: true,
				imgUrl: '',
				nickname: '',
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {},
				pageHeight: 500,
				newsList: [],
				top: 0, //提示,到顶部的距离
				beforeid: '',
				endid: '',
				count: '',
				time: '早安',
				data: [],
				banner: '../../static/img/banner.png',
				ano: '',
				tempnewsList: [],
				isLogin: false, // false true 是否登录过
				isCheck: false, // false true 是否打卡过
				tempdata: [],
			};
		},
		onLoad(options) {
			const that = this;
			// test
			this.getBanner();
			this.getTime();

			// this.getBannerList();
			this.ano = options.ano;
			// 在页面onLoad回调事件中创建插屏广告实例
			if (wx.createInterstitialAd) {
				interstitialAd = wx.createInterstitialAd({
					adUnitId: that.$apis.adUnitId
				})
			}
		},
		onReady() {
			if (this.ano) {
				this.upCallback(0);
				uni.navigateTo({
					url: '/pages/news/index?ano=' + this.ano
				})
			}
		},
		onShow() {
			// 是否保存过
			let nickname = uni.getStorageSync('nickname');
			this.isLogin = nickname ? true : false;
			if (interstitialAd && !this.$apis.testMode) {
				interstitialAd.show().catch((err) => {
					console.error(err)
				})
			}
		},
		onPageScroll(e) {
			let scrollTop = e.scrollTop;
			if (scrollTop > 0) {
				this.pageHeight = 210;
			} else {
				this.pageHeight = 500;
			}
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: this.newsList[0].title,
				path: '/pages/index/index',
				imageUrl: '',
			}
		},
		methods: {
			gotoAlbum() {
				uni.redirectTo({
					// url: '/pages/album/index?templateid=1&showano=806eb8c3ac15f6b62fbc2211a617488e&ano=Y0PYVO879&title=快乐的童年'
					url: '/pages/album/index?templateid=2&showano=de46f70da2d43962fbdf7ac4084fba7d&ano=hzheBp520&title=%E7%B2%BD%E6%83%85%E7%AB%AF%E5%8D%88'
				});
				// '/pages/make/make?templateid=1&ano=0&title=快乐的童年'
			},
			chooseavatar(e) {
				this.head = e.target.avatarUrl
			},
			getBanner() {
				const that = this;
				this.$apis.wxmessage({
					appid: appId
				}).then(res => {
					that.imgUrl = res.banner;
				}).catch(() => {});
			},
			// 订阅推送
			subscribe() {
				console.log('订阅推送', 1);
				uni.requestSubscribeMessage({
					tmplIds: ['pew_Y4QvWXDd1psA7xnymIST2jERtbGh3kscftZRX6c'],
					complete: (res) => {
						console.log(res, res);
					}
				})
			},
			/*下拉刷新的回调 */
			downCallback() {
				let item = this.newsList[0];
				if (!item) return false
				let beforeid = this.newsList[0].fixedtime;
				let data = {
					'page': 1,
					'beforeid': beforeid
				};
				this.$apis.getIndexlist(data).then(res => {
					this.mescroll.endSuccess();
					if (res.data.code == 1) {
						for (var i = 0; i < res.data.newslist.length; i++) {
							var j = res.data.newslist.length - i - 1;
							this.newsList.unshift((res.data.newslist[j]));
							this.data[0].newslist.unshift(res.data.newslist[j]);
						}
					}
					this.count = res.data.count;
					//显示提示
					// #ifdef H5
					this.top = uni.upx2px(100 + 88) + "px"; // H5的高度需加上 88的标题栏
					// #endif

					// #ifndef H5
					this.top = uni.upx2px(0) + "px"; // 非H5不必加
					// #endif
					setTimeout(() => {
						this.top = 0;
					}, 2000);
				}).catch(() => {
					this.mescroll.endErr();
				});
				//this.mescroll.resetUpScroll()

			},
			upCallback(page) {
				this.pagenow = page.num;
				let length = this.newsList.length;
				let endid = 0;
				if (length > 0) {
					endid = this.newsList[length - 1].fixedtime;
				}
				if (page.num > 6) {
					this.mescroll.endUpScroll(1)
					return false;
				}

				let data = {
					'page': page.num,
					'pageize': page.size,
					'endid': endid
				};
				var that = this;
				this.$apis.getIndexlist(data).then(res => {
					this.mescroll.endSuccess(res.data.newslist.length);
					this.newsList = this.newsList.concat(res.data.newslist);
					this.data = this.data.concat(res.data);
					this.$nextTick(() => {
						this.showMore = false
					})
				}).catch(() => {
					this.mescroll.endErr();
				});
			},
			clickToDetail: function(index, type) {
				console.log('index', index);
				uni.navigateTo({
					url: '/pages/news/index?ano=' + index
				})
			},
			// banner图片获取
			getBannerList: function() {
				let data = {};
				let that = this;
				that.$apis.getBanner(data).then(res => {
					//追加新数据
					that.banner = res.banner;
				})
			},
			// 准备打卡
			checkToday() {
				// 判断是否已登录
				if (this.isLogin) {
					// 直接签到
					this.checkFunc()
				} else {
					// 获取用户信息
					this.getUserInfo()
				}
			},
			// 直接签到  今日打卡
			checkFunc() {
				wx.showToast({ // 显示加载中loading效果2秒
					title: '打卡中',
					icon: 'loading',
					duration: 2000 //提示的延迟时间
				});
				this.isCheck = true;
			},
			getUserInfo() {
				this.$refs.popup.open('top')
			},
			hidePop() {
				this.$refs.popup.close('top')
			},
			// 获取昵称
			onNickName(e) {
				console.log('e', e.detail.value);
				this.nickname = e.detail.value
			},
			// 用户个人信息
			submit() {
				if (!this.nickname) {
					wx.showModal({
						title: '提示',
						content: '用户昵称不能为空',
						modalType: false,
					})
					return false
				}
				// 信息保存
				uni.setStorageSync('head', this.head);
				uni.setStorageSync('nickname', this.nickname);
				this.checkFunc();
				this.hidePop();
			},

			// 获取时间
			getTime() {
				// 获取当前时间
				let now = new Date();
				// 获取当前小时数
				let hour = now.getHours();
				// 判断当前时间是早上、中午还是下午、晚上
				if (hour >= 5 && hour < 12) {
					this.time = "早安"
				} else if (hour >= 12 && hour < 18) {
					this.time = "午安"
				} else {
					this.time = "晚安"
				}
			}
		},
	}
</script>
<style>
	@import url("index.css");
</style>